/**
 * @Description: 26. 删除有序数组中的重复项
 * 给你一个 升序排列 的数组 nums ，请你 原地 删除重复出现的元素，
 * 使每个元素 只出现一次 ，返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
 * @Date:Create in 15:47  2022/5/13
 */
public class removeDuplicates0026 {

    public static void main(String[] args) {
        int[] nums = new int[]{0,0,1,1,1,2,2,3,3,4};
        int a = removeDuplicates2(nums);
        System.out.println(a);
    }
    //双指针法
    public static int removeDuplicates1(int[] nums) {
        int size = nums.length;
        int slowindex = 0,sum=1;
        for (int fastindex = 0; fastindex < size-1; fastindex++) {
            if(nums[fastindex] != nums[fastindex+1]){
                nums[++slowindex] = nums[fastindex+1];
                sum++;
            }
            System.out.println(nums[slowindex]);
        }

        return sum;
    }

    //优化
    public static int removeDuplicates2(int[] nums) {
        int size = nums.length;
        int slowindex = 0,sum=1;
        for (int fastindex = 0; fastindex < size; fastindex++) {
            if(nums[fastindex] != nums[slowindex]){
                if(fastindex-slowindex>1){
                    nums[++slowindex] = nums[fastindex];
                }
            }
            System.out.println(nums[slowindex]);
        }

        return slowindex+1;
    }
}
